<?php

namespace app\common\model;

use think\Model;
use think\Db;

/**
 * 拼团开团模型
 */
class TeamFound extends Model
{
    protected $append = [
        'daojishi_text',
        'shengyuminge_text',
        'status_text',
        'team_follow_text',
        'goods_info_text'


    ];

    public function initialize()
    {
        $team_found_num = Db::name('team_found')->where('found_end_time', '<', time())->where('status', 1)->count();

        //修改开团状态为 拼团失败
        if ($team_found_num > 0) {

            $teamFound = Db::name('team_found')->where('found_end_time', '<', time())->where('status', 1)->select();

            Db::name('team_found')->where('found_end_time', '<', time())->where('status', 1)->update(['status' => 3]);
            $teamOrderLogic = new \app\common\logic\TeamOrderLogic();

            foreach ($teamFound as $k => $v) {

                $order = \app\common\model\Order::get($v['order_id']);

                $teamOrderLogic->setOrder($order);
                $teamOrderLogic->setUserId($v['user_id']);

                $teamOrderLogic->setTeamFound(collection($v));
                $teamOrderLogic->refundTeamOrder(); //成团失败退款
            }

        }
    }


    public function order()
    {
        return $this->belongsTo('Order', 'order_id', 'id')->setEagerlyType(0);
    }

    public function teamFollow()
    {
        return $this->hasMany('TeamFollow', 'found_id', 'id');
    }

    public function teamActivity()
    {
        return $this->belongsTo('TeamActivity', 'team_id', 'id')->setEagerlyType(0);
    }

    //拼单节省了多少钱
    public function getCutPriceAttr($value, $data)
    {
        return $data['goods_price'] - $data['price'];
    }

    //剩余多少名额
    public function getShengyumingeTextAttr($value, $data)
    {
        return $data['need'] - $data['join'];
    }

    //状态描述
    public function getStatusTextAttr($value, $data)
    {
        $status = ['0' => '待开团', '1' => '已开团', '2' => '拼团成功', '3' => '拼团失败'];
        return $status[$data['status']];
    }


    //倒计时
    public function getDaojishiTextAttr($value, $data)
    {
        $res = $data['found_end_time'] - time();
        if ($res > 0) {
            return $res;
        } else {
            return 0;
        }

    }

    //参团用户列表
    public function getTeamFollowTextAttr($value, $data)
    {
        $teamFollowList = [];

        $teamFoundUser = ['follow_user_nickname' => $data['nickname'], 'follow_user_avatar' => $data['avatar']];
        array_push($teamFollowList,$teamFoundUser);

        $teamFollowLists = Db::name('team_follow')->where(['found_id' => $data['id'], 'status' => ['<>','0']])->field('follow_user_nickname,follow_user_avatar')->select();


        foreach($teamFollowLists as $k => $v){

           array_push($teamFollowList,$v);
        }
//
//        array_unshift($teamFollowList, ['follow_user_nickname' => $data['nickname'], 'follow_user_avatar' => $data['avatar']]);
        return $teamFollowList;
    }

    public function getGoodsInfoTextAttr($value, $data)
    {
        $teamM = new \app\common\model\TeamActivity();
        $goodsM = new \app\common\model\Goods();
        $team = $teamM->get($data['team_id']);
        $goods = $goodsM->where(['id' => $team['goods_id']])->field('id,name,market_price,image')->find();
        return $goods->toArray();
    }


}